package com.flute.haflute.jobbox.common.tools;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Enumeration;

/**
 * 系统平台相关的处理，待完备
 * 
 */
public class OSUtil {

	public static boolean isWindowsOS() {
		boolean isWindowsOS = false;
		String osName = System.getProperty("os.name");
		if (osName.toLowerCase().indexOf("windows") > -1) {
			isWindowsOS = true;
		}
		return isWindowsOS;
	}

	public static InetAddress getLocalHostAddress(boolean needPublicAddress,
			String configuredLocalIp) {
		InetAddress ip = null;
		InetAddress foundIp = null;
		try {
			if (isWindowsOS()) {
				ip = InetAddress.getLocalHost();
			} else {
				boolean bFindIP = false;
				Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
						.getNetworkInterfaces();
				while (netInterfaces.hasMoreElements()) {
					if (bFindIP) {
						break;
					}
					NetworkInterface ni = (NetworkInterface) netInterfaces
							.nextElement();
					Enumeration<InetAddress> ips = ni.getInetAddresses();
					while (ips.hasMoreElements()) {
						foundIp = (InetAddress) ips.nextElement();
						if (configuredLocalIp != null
								&& configuredLocalIp.equals(foundIp
										.getHostAddress())) {
							ip = foundIp;
							bFindIP = true;
							break;
						}
						if (needPublicAddress && !foundIp.isSiteLocalAddress()
								&& !foundIp.isLoopbackAddress() // 外网地址ַ
								&& foundIp.getHostAddress().indexOf(":") == -1) {
							ip = foundIp;
							if (configuredLocalIp == null) {
								bFindIP = true;
								break;
							}
						} else if (foundIp.isSiteLocalAddress()
								&& !foundIp.isLoopbackAddress() // 内网地址ַ
								&& foundIp.getHostAddress().indexOf(":") == -1) {
							ip = foundIp;
							if (configuredLocalIp == null) {
								bFindIP = true;
								break;
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ip;
	}

	public static String getLocalIP(boolean needPublicAddress) {
		String sIP = "";
		InetAddress ip = getLocalHostAddress(needPublicAddress, null);
		if (ip != null) {
			sIP = ip.getHostAddress();
		}
		return sIP;
	}

	public static boolean isLocalAddress(String originIp) {
		if (originIp == null || originIp.length() == 0)
			return false;

		InetAddress[] allLocalAddress = getAllInterfaces();
		for (InetAddress address : allLocalAddress) {
			if (address.getHostAddress().equals(originIp))
				return true;
		}

		return false;
	}

	public static InetAddress[] getAllInterfaces() {
		ArrayList<InetAddress> addresses = new ArrayList<InetAddress>();
		try {

			Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
					.getNetworkInterfaces();
			while (netInterfaces.hasMoreElements()) {
				NetworkInterface ni = (NetworkInterface) netInterfaces
						.nextElement();
				Enumeration<InetAddress> ips = ni.getInetAddresses();
				while (ips.hasMoreElements()) {
					InetAddress address = (InetAddress) ips.nextElement();
					addresses.add(address);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return addresses.toArray(new InetAddress[addresses.size()]);
	}

	public static void main(String[] args) {
		String serverIP = OSUtil.getLocalIP(true);
		System.out.println("public serverIP:::" + serverIP);
		OSUtil.getLocalHostAddress(false, null);
		InetAddress[] all = OSUtil.getAllInterfaces();
		for (InetAddress address : all) {
			System.out.println("1 address=" + address);
		}
	}
}
